﻿using MediatR;
using ProductionManagement.Application.Command.PreProductionCommand;
using ProductionManagement.Domain.Dtos.PreProductionDtos;
using ProductionManagement.Domain.ProductionDomain;
using ProductionManagement.Infrstructre.lnterface;
using System.Linq;

namespace ProductionManagement.Application.CommandHandler.PreProductionCommandHandler
{
    public class PreProductionFanCommandHandler : IRequestHandler<PreProductionFanCommand, FanPreProductionDto>
    {
        private readonly IPreProducsRepository _preProducsRep;
        private readonly IPreProductionRepository _preProductionRep;
        public PreProductionFanCommandHandler(IPreProducsRepository preProducsRep, IPreProductionRepository preProductionRep)
        {
            this._preProducsRep = preProducsRep;
            this._preProductionRep = preProductionRep;
        }

        public async Task<FanPreProductionDto> Handle(PreProductionFanCommand request, CancellationToken cancellationToken)
        {
            var list = (from a in await _preProductionRep.GetAll() select a).Where(x => x.PreProductionId == request.PreProductionId).ToList();
            var item = (from b in await _preProducsRep.GetAll() select b).ToList();
            var result = (from c in list
                          select new FanPreProductionDto
                          {
                              PreProductionId = c.PreProductionId,
                              PPDocuNumber = c.PPDocuNumber,
                              PPName = c.PPName,
                              PPNumTotal = c.PPNumTotal,
                              PPType=c.PPType,
                              PPState = c.PPState,
                              PPGeneratedNum = c.PPGeneratedNum,
                              PPSmark = c.PPSmark,
                              AddName = c.AddName,
                              AddTime = c.AddTime,
                              UpdName=c.UpdName,
                              UpdTime=c.UpdTime,
                              Office_Id = c.Office_Id,
                              User_Id = c.User_Id,
                              FanDto = (from d in item
                                        where d.PPDocuNumber == c.PPDocuNumber
                                        select new FanPreProductionDetailDto
                                        {
                                            PreProducsId = d.PreProducsId,
                                            PPDocuNumber = d.PPDocuNumber,
                                            ProductId = d.ProductId,
                                            PPsNum = d.PPsNum,
                                            PPsDeliveryDate = d.PPsDeliveryDate,
                                            PNumber = d.PNumber,
                                            PName = d.PName,
                                            PSpec = d.PSpec,
                                            PUnit = d.PUnit,
                                            PPsSmark = d.PPsSmark,
                                        }).ToList()
                          });
            return result.FirstOrDefault();
        }
    }
}
